import { createRouter, createWebHashHistory, } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import WindowsLoading from '../views/Windows/WindowsLoading.vue'
import WindowsHome from '../views/Windows/WindowsHome.vue'
import WindowsDemandList from '../views/Windows/WindowsDemandList.vue'
import WindowsDemandConfirmation from '../views/Windows/WindowsDemandConfirmation.vue'
import WindowsType from '../views/Windows/WindowsType.vue'
import WindowsPersonnel from '../views/Windows/WindowsPersonnel.vue'
import WindowsAddDemand from '../views/Windows/WindowsAddDemand.vue'
import WindowsRegIt from '../views/Windows/WindowsRegIt.vue'
import WindowsRecordDemand from '../views/Windows/WindowsRecordDemand.vue'
import WindowsUpdateUser from '../views/Windows/WindowsUpdateUser.vue'
import WindowsContingencyPlan from '../views/Windows/WindowsContingencyPlan.vue'
import WindowsContingencyPlanOperation from '../views/Windows/WindowsContingencyPlanOperation.vue'
import WindowsContingencyPlanHistory from '../views/Windows/WindowsContingencyPlanHistory.vue'

import AndroidLoading from '../views/Android/AndroidLoading.vue'
import AndroidHome from '../views/Android/AndroidHome.vue'
import AndroidHomePage from '../views/Android/AndroidHomePage.vue'
import AndroidRegIt from '../views/Android/AndroidRegIt.vue'
import AndroidAddDemand from '../views/Android/AndroidAddDemand.vue'
import AndroidDemandList from '../views/Android/AndroidDemandList.vue'
import AndroidDemandInfo from '../views/Android/AndroidDemandInfo.vue'
import AndroidPersonnel from '../views/Android/AndroidPersonnel.vue'
import AndroidType from '../views/Android/AndroidType.vue'
import AndroidDemandConfirmation from '../views/Android/AndroidDemandConfirmation.vue'
import AndroidRecordDemand from '../views/Android/AndroidRecordDemand.vue'
import AndroidUpdateUser from '../views/Android/AndroidUpdateUser.vue'
import AndroidContingencyPlan from '../views/Android/AndroidContingencyPlan.vue'
import AndroidContingencyPlanOperation from '../views/Android/AndroidContingencyPlanOperation.vue'

import ChatUi from '../views/ChatUi.vue'
import axios from '@/utils/axios'

import RefView from '../views/RefView.vue'
import ShowContingencyPlan from '../views/ShowContingencyPlan.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
  },
  {
    path: '/WindowsLoading',
    name: 'WindowsLoading',
    component: WindowsLoading,
  },
  {
    path: '/WindowsHome',
    name: 'WindowsHome',
    component: WindowsHome,
    children: [
      {
        path: '',
        name: 'WindowsDemandListDef',
        component: WindowsAddDemand,
      },
      {
        path: 'WindowsDemandList',
        name: 'WindowsDemandList',
        component: WindowsDemandList,
      },
      {
        path: 'WindowsDemandConfirmation',
        name: 'WindowsDemandConfirmation',
        component: WindowsDemandConfirmation,
      },
      {
        path: 'WindowsType',
        name: 'WindowsType',
        component: WindowsType,
      },
      {
        path: 'WindowsPersonnel',
        name: 'WindowsPersonnel',
        component: WindowsPersonnel,
      },
      {
        path: 'WindowsAddDemand',
        name: 'WindowsAddDemand',
        component: WindowsAddDemand,
      },
      {
        path: 'WindowsRegIt',
        name: 'WindowsRegIt',
        component: WindowsRegIt,
      },
      {
        path: 'WindowsChatUi',
        name: 'WindowsChatUi',
        component: ChatUi
      },
      {
        path: 'WindowsRecordDemand',
        name: 'WindowsRecordDemand',
        component: WindowsRecordDemand
      },
      {
        path: 'WindowsUpdateUser',
        name: 'WindowsUpdateUser',
        component: WindowsUpdateUser
      },
      {
        path: 'WindowsContingencyPlan',
        name: 'WindowsContingencyPlan',
        component: WindowsContingencyPlan
      },
      {
        path: 'WindowsContingencyPlanOperation',
        name: 'WindowsContingencyPlanOperation',
        component: WindowsContingencyPlanOperation
      },
      {
        path: 'WindowsContingencyPlanHistory',
        name: 'WindowsContingencyPlanHistory',
        component: WindowsContingencyPlanHistory
      }
    ]
  },
  {
    path: '/AndroidLoading',
    name: 'AndroidLoading',
    component: AndroidLoading,
  },
  {
    path: '/AndroidHome',
    name: 'AndroidHome',
    component: AndroidHome,
    children: [
      {
        path: 'AndroidHomePage',
        name: 'AndroidHomePage',
        component: AndroidHomePage,
      },
      {
        path: 'AndroidRegIt',
        name: 'AndroidRegIt',
        component: AndroidRegIt
      },
      {
        path: 'AndroidAddDemand',
        name: 'AndroidAddDemand',
        component: AndroidAddDemand,
      },
      {
        path: 'AndroidDemandList',
        name: 'AndroidDemandList',
        component: AndroidDemandList,
      },
      {
        path: 'AndroidDemandInfo',
        name: 'AndroidDemandInfo',
        component: AndroidDemandInfo,
      },
      {
        path: 'AndroidPersonnel',
        name: 'AndroidPersonnel',
        component: AndroidPersonnel
      },
      {
        path: 'AndroidType',
        name: 'AndroidType',
        component: AndroidType
      },
      {
        path: 'AndroidDemandConfirmation',
        name: 'AndroidDemandConfirmation',
        component: AndroidDemandConfirmation
      },
      {
        path: 'AndroidChatUi',
        name: 'AndroidChatUi',
        component: ChatUi
      },
      {
        path: 'AndroidRecordDemand',
        name: 'AndroidRecordDemand',
        component: AndroidRecordDemand
      },
      {
        path: 'AndroidUpdateUser',
        name: 'AndroidUpdateUser',
        component: AndroidUpdateUser
      },
      {
        path: 'AndroidContingencyPlan',
        name: 'AndroidContingencyPlan',
        component: AndroidContingencyPlan
      },
      {
        path: 'AndroidContingencyPlanOperation',
        name: 'AndroidContingencyPlanOperation',
        component: AndroidContingencyPlanOperation
      },
    ]
  },
  {
    path: '/RefView',
    name: 'RefView',
    component: RefView
  },
  {
    path: '/ShowContingencyPlan',
    name: 'ShowContingencyPlan',
    component: ShowContingencyPlan
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})
// import { START_LOCATION } from 'vue-router'
router.beforeEach((to, from, next) => {
  if (to.query.ObjectList != "" && to.query.ObjectList != null && to.query.code != null && to.query.code != "") {
    var ObjectList = JSON.parse(decodeURIComponent(to.query.ObjectList))
    if (ObjectList.RYD_LoginId != null && ObjectList.RYD_LoginId != '') {

      axios({
        method: 'put',
        url: "/api/QRLogin",
        data: {
          RYD_LoginId: ObjectList.RYD_LoginId,
          WX_Code: to.query.code,
          returnUrl: ObjectList.returnUrl
        }
      }).then(res => {
        if (res != null && res != "") {
          alert("扫码成功");

        } else {
          alert("扫码失败")
        }
        try {
          // eslint-disable-next-line no-undef
          WeixinJSBridge.call('closeWindow');
          return false;
        } catch (error) {
          var userAgent = navigator.userAgent;
          if (userAgent.indexOf("Firefox") != -1 || userAgent.indexOf("Chrome") != -1) {
            window.location.href = "about:blank";
            window.close();
          } else {
            window.opener = null;
            window.open("", "_self");
            window.close();
          }
          return false;
        }
      }).error(() => {
        alert("扫码失败")
        return false;
      })

    }
  }
  if (to.name == "AndroidLoading" && from.name == "AndroidHomePage") {
    router.back();
  } else if (to.name == "WindowsLoading" && (from.name != null)) {
    router.back();
  } else {
    next();
  }

  // next()
  // let searchStr = location.search.trim();
  // if (from == START_LOCATION&&to.path=="/" && searchStr != "") {
  //   // url = str.substring(0, num);
  //   searchStr=searchStr.substring(1);
  //   let arr = searchStr.split("&")
  //   let code = "";
  //   let RYD_DemandId = "";
  //   arr.forEach(item => {
  //     let query = item.split("=")
  //     if (query[0] == "code") {
  //       code = query[1];
  //     }
  //     if (query[0] == "RYD_DemandId") {
  //       RYD_DemandId = query[1];
  //     }
  //   })
  //   if (RYD_DemandId!=""&&RYD_DemandId!=null) {
  //     sessionStorage.setItem("RYD_DemandId",RYD_DemandId);
  //   }
  //   if (code != "") {
  //     // location.href = url;
  //     next({
  //       name: "WXLogin",
  //       params: {
  //         code: code,
  //       }
  //     });
  //   }
  // } else if(searchStr == "") {
  //   // ...
  //   // 返回 false 以取消导航
  //   if (to.name == 'home'||to.name == 'WXLogin') {
  //     let EquipmentInfo = navigator.userAgent
  //     if (EquipmentInfo.indexOf('Windows') >= 0) {
  //       next({ name: 'WindowsLoading' })
  //     } else if (EquipmentInfo.indexOf('Android') >= 0 || EquipmentInfo.indexOf('iPhone') >= 0) {
  //       next({ name: 'AndroidLoading' })
  //     } else {
  //       next({ name: 'WindowsLoading' })
  //     }
  //   } else if (to.name == 'AndroidLoading' && from.fullPath != '/') {
  //     resetRouter();
  //     router.back();
  //   } else if (to.name == null) {
  //     console.log('错误网址');
  //     return false;
  //   }
  //   else {
  //     next();
  //   }
  // }else if(to.name == 'WXLogin'){
  //   next();
  // }
})
router.onError(res => {
  console.log(res);
})
export function resetRouter() {
  const newRouter = createRouter({
    history: createWebHashHistory(),
    routes,
  })
  router.matcher = newRouter.matcher // the relevant part
}
export default router
